package com.vinjogames.util;

import com.vinjogames.gfx.AnimationManager;
import com.vinjogames.som.entities.Game;

/**
 * @author v-vince
 * Threads:
 * 1:  Renderer Thread, controlled by GameRenderer  (already provided)
 * 		handles all screen drawing
 * 2:  Main Thread, controlled by Activity (already provided)
 * 		touch screen events mostly
 * 3:  Game Thread, this class 
 * 		should handle game calculations (physics, ai, collision, animation)
 *
 */
public class GameThread extends Thread {

	private Game mGame;
	private GameClock mClock = GameClock.getInstance();
	private boolean mRunning;
	
	private AnimationManager animMgr;

	public GameThread(Game game){
		mClock.setTime(0);
		mGame = game;
		animMgr = AnimationManager.getInstance();
		Logger.log("[GameThread]: Constructed");
	}

	@Override
	public void run() {
		while(mRunning){
			mClock.tick();
			//animMgr.tick(mClock.getTime());
		}
	}

	public void pause(){
		if(mRunning){
			mClock.pause();
			mRunning = false;
		}
	}

	public void unpause(){
		if(!mRunning){
			mClock.resume();
			mRunning = true;
		}
	}

	public void close(){
		System.gc();
		pause();
		boolean retry = true;
		while (retry) {
			try {
				join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}
}
